package cn.bdqn.controller;

import cn.bdqn.bean.result.Result;
import cn.bdqn.bean.result.ResultGenerator;
import cn.bdqn.domain.DoctorDiagnose;
import cn.bdqn.domain.Menber;
import cn.bdqn.domain.Recipe;
import cn.bdqn.domain.Registration;
import cn.bdqn.service.DiagnoseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/doctor/diagnose")
public class DiagnoseController {

    @Autowired
    private DiagnoseService diagnoseService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据科室id查询在此科室挂号 的所有人 (挂号受理未诊断)
     * @param deptId
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping("/getRegistersByDeptId/{deptId}")
    public Result getRegistrations( @PathVariable Integer deptId, Map<String,Object> map) throws Exception {
        try {
            //查询当前科室的所有挂号人
            List<Registration> registrationList = diagnoseService.queryByDeptId(deptId);
            //添加到结果集
            map.put("registerList",registrationList);
            return ResultGenerator.genSuccessResult(map);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询错误");
        }

    }


    /**
     * 查询挂号人的历史处方以及挂号信息
     * @param regId
     * @param deptId
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping("/getRegistrationInfo/{regId}/{deptId}")
    public Result getRegistrationInfo(@PathVariable String regId,@PathVariable Integer deptId,Map<String,Object> map) throws Exception {

        try {
            //查询查询指定的挂号人
            Registration registration = diagnoseService.queryByIdAndDeptId(regId,deptId);

            //判断这个病人的挂号信息是否存在
            if (registration != null){
                //查询redis里面有没有这个id信息
                String accountJson = (String) redisTemplate.boundValueOps("RegistrationId").get();
                //如果没有或者和当前的挂号id不同就修改或添加当前id
                if (accountJson == null || !accountJson.equals(regId)){
                    accountJson = regId;
                    redisTemplate.boundValueOps("RegistrationId").set(accountJson);
                }

                //添加到结果集
                map.put("registration",registration);
                //查询这个病人的历史处方
                List<Recipe> recipes = diagnoseService.queryByRegisterId(regId);
                //查询这个病人的历史诊断记录
                List<DoctorDiagnose> doctorDiagnoses = diagnoseService.queryDiagnosesByRegId(regId);
                //判断是否有历史处方
                if (recipes.size() > 0){
                    map.put("recipes",recipes);
                }
                //判断是否有历史诊断记录
                if(doctorDiagnoses.size() > 0){
                    map.put("diagnoses",doctorDiagnoses);
                }
                return ResultGenerator.genSuccessResult(map);
            }else {
                return ResultGenerator.genFailResult("没有此人挂号信息");
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("异常问题");
        }
    }


    /**
     * 添加一条诊断信息
     * @param doctorDiagnose
     * @return
     * @throws Exception
     */
    @RequestMapping("/addDiagnose")
    public Result addDiagnose(DoctorDiagnose doctorDiagnose) throws Exception {

        try {
            int res = diagnoseService.addDiagnose(doctorDiagnose);
            if (res != 0){
                return ResultGenerator.genFailResult("已添加成功");
            }else {
                return ResultGenerator.genFailResult("诊断添加失败!");
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("系统错误!");
        }
    }

}
